package com.itgungnir.designpattern.state;

public class MoneyInsertedState implements State {
	private CandyMachine candyMachine;

	public MoneyInsertedState(CandyMachine candyMachine) {
		this.candyMachine = candyMachine;
	}

	@Override
	public void onMoneyInserted() {
		System.out.println("不能多次投币！");
	}

	@Override
	public void onMoneyReturned() {
		System.out.println("正在退钱......退钱成功！");
		candyMachine.setState(candyMachine.getNoMoneyState());
	}

	@Override
	public void onCrankTurned() {
		System.out.println("转动了摇把......");
		int random = (int) (Math.random() * 10000);
		if (random % 10 == 0 && candyMachine.getCandyCount() > 1) {
			System.out.println("恭喜您获得了两颗糖果！");
			candyMachine.setState(candyMachine.getWinnerState());
			candyMachine.popCandy();
		} else {
			if (candyMachine.getCandyCount() > 0) {
				candyMachine.setState(candyMachine.getCandyPoppingState());
				candyMachine.popCandy();
			} else {
				candyMachine.setState(candyMachine.getNoCandyState());
				candyMachine.popCandy();
			}
		}
	}

	@Override
	public void onCandyPoped() {
		System.out.println("请先转动摇把！");
	}
}